package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.AirQualityIndex;
import com.qf.mapper.AirQualityIndexMapper;
import com.qf.service.AirQualityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 10537
 * @version V1.0
 * @Date 2021/10/4 16:41
 */

@Service
public class AirQualityServiceImpl implements AirQualityService {
    @Autowired
    AirQualityIndexMapper airQualityIndexMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void add(AirQualityIndex airQualityIndex) {
        int i=airQualityIndexMapper.insert(airQualityIndex);
        if (i!=1){
            System.out.println("添加空气质量信息失败");
            throw new RuntimeException("添加空气质量信息失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void remove(Integer id) {
        int i=airQualityIndexMapper.deleteByPrimaryKey(id);
        if (i!=1){
            System.out.println("删除空气质量信息失败");
            throw new RuntimeException("删除空气质量信息失败");
        }
    }


    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void modify(AirQualityIndex airQualityIndex) {
        int i=airQualityIndexMapper.updateByPrimaryKey(airQualityIndex);
        if (i!=1){
            System.out.println("修改空气质量信息失败");
            throw new RuntimeException("修改空气质量信息失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public AirQualityIndex findById(Integer id) {
        return airQualityIndexMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public PageInfo<AirQualityIndex> findByDistrictIdAndPage(Integer page, Integer rows, Integer districtId) {
        PageHelper.startPage(page,rows);
        List<AirQualityIndex> list=airQualityIndexMapper.selectByDistrictId(districtId);
        PageInfo<AirQualityIndex> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
}
